<!-- TODO -->
<template>
    <div>
        <el-card class="!border-none mt-4" shadow="never">
            <el-button v-perms="['auth.admin/add']" type="primary" @click="handleAdd">
                新增等级
            </el-button>

            <el-table size="large" class="mt-[14px]" :data="pager.lists">
                <el-table-column label="等级级别" prop="level">
                    <template #default="{ row }">
                        {{ row.level }}级
                    </template>
                </el-table-column>
                <el-table-column label="等级名称" prop="name" />
                <el-table-column label="提成比例">
                    <template #default="{ row }"> {{ row.commission }}% </template>
                </el-table-column>
                <el-table-column label="团长数量" prop="teamhead_num" />
                <el-table-column label="操作" fixed="right" min-width="160">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)"> 编辑 </el-button>
                        <el-button type="primary" link @click="handleDelete(row.id)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { teamheadlevelLists, Delteamheadlevel } from '@/api/teamhead'
import EditPopup from './edit.vue'
import feedback from '@/utils/feedback'

const { pager, getLists } = usePaging({
    fetchFun: teamheadlevelLists
})
getLists()
//删除
const handleDelete = async (id: any) => {
    await feedback.confirm('确定要删除？')
    await Delteamheadlevel({ id })
    getLists()
}

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}
</script>
<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}
</style>
